"use strict";

app.controller("sysConfigCtrl", [
    "$scope",
    "$http",
    "$stateParams",
    "$rootScope",
    "editableOptions",
    "editableThemes",
    function ($scope,
              $http,
              $stateParams,
              $rootScope,
              editableOptions,
              editableThemes) {
        editableThemes.bs3.inputClass = "input-sm";
        editableThemes.bs3.buttonsClass = "btn-sm";
        editableOptions.theme = "bs3";

        $scope.setBreadcrumb = function (type) {
            $rootScope.initBreadcrumb($scope.breadcrumb);
            if (sysConfigName[type]) {
                angular.forEach(sysConfigName[type], function (name) {
                    $scope.breadcrumb.push(name);
                })
            } else {
                $scope.breadcrumb.push('基础配置');
            }
        }

        $scope.info = {};
        $scope.info.configList = [];
        $scope.info.type = $stateParams.type;
        $scope.info.newType = "";
        $scope.setBreadcrumb($scope.info.type);
        //是否显示下拉框
        $scope.info.showSelect = false;
        $scope.info.selectData = [];
        $scope.info.selectInfo = {};
        if ($stateParams.type == "Component") {
            $scope.info.showSelect = true;
            $.ajax({
                type: "get",
                url: sys_config_url + "/component/getList",
                dataType: "JSON",
                async: false,
                success: function (data) {
                    $scope.info.selectData = data.data;
                    $scope.info.type = $scope.info.selectData[0].key;
                    $scope.info.selectInfo = $scope.info.selectData[0];
                }
            });
            $scope.selectChange = function () {
                $scope.info.type = $scope.info.selectInfo.key;
                $scope.getSysConfig();
            };
        }

        //新增类型
        $scope.addType = function () {
            if ($scope.info.newType == "") {
                alert("请输入新增组件名称!");
                return;
            }

            for (var i = 0; i < $scope.info.selectData.length; i++) {
                if ($scope.info.selectData[i].key == $scope.info.newType) {
                    alert("组件" + $scope.info.newType + "已存在!");
                    return;
                }
            }
            var obj = {
                key: $scope.info.newType,
                value: $scope.info.newType
            };
            $scope.info.selectData.push(obj);
            $scope.info.selectInfo = obj;
            $scope.selectChange();
        };

        //清空header数据
        $rootScope.clean($scope.app);
        $rootScope.initHeaderElements($scope.settings);
        //初始化header数据
        $scope.settings.btnEle.push({
            show: true,
            label: "新增key",
            func: function () {
                $scope.add();
            }
        });
        $scope.$on("$destroy", function () {
            $scope.app.page = {};
            $rootScope.initHeaderElements($scope.settings);
        });

        //获取配置列表
        $scope.getSysConfig = function () {
            var url = sys_config_url + "/select/get?type=" + $scope.info.type;
            if ($scope.info.showSelect) {
                url = sys_config_url + "/component/get?type=" + $scope.info.type;
            }
            $http.get(url).success(function (data) {
                if (data.code == 0) {
                    $scope.info.configList = data.data[$scope.info.type];
                } else {
                    alert(data.desc);
                }
            });
        };
        $scope.getSysConfig();

        $scope.check = function (data) {
            if (undefined == data || null == data || data.trim() == "") {
                return "不能为空";
            }
        };

        $scope.save = function () {
            var info = {};
            info.titleData = "提示";
            info.bodyData = "确定要保存修改吗？";
            $rootScope.confirmWindow(info, $scope.saveConfig());
        };
        $scope.remove = function (index) {
            var info = {};
            info.titleData = "提示";
            info.bodyData = "确定要删除该配置项么吗？";
            $rootScope.confirmWindow(info, function () {
                $scope.info.configList.splice(index, 1);
                $scope.saveConfig();
            });
        };

        $scope.saveConfig = function () {
            console.log(
                "type:" +
                $scope.info.type +
                ";list:" +
                JSON.stringify($scope.info.configList)
            );
            var url = sys_config_url + "/select/save";
            if ($scope.info.showSelect) {
                url = sys_config_url + "/component/save";
            }
            $http
                .post(url, {
                    type: $scope.info.type,
                    list: JSON.stringify($scope.info.configList)
                })
                .success(function (data) {
                    if (data.code == 0) {
                        $scope.getSysConfig();
                    } else {
                        alert(data.desc);
                    }
                });
        };

        $scope.add = function () {
            var obj = new Object();
            obj.key = null;
            obj.value = null;
            $scope.info.configList.push(obj);
        };
    }
]);
